package shortcircuit.graphics;

import shortcircuit.Display;
import shortcircuit.engine.PuzzleEngine;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;

/**
 * This class is the puzzle level screen and directly extends JPanel, which means
 * it can be added directly to the Display as a JPanel. It holds a ListPanel of
 * level buttons, which can be clicked to initialize a PuzzleEngine for that
 * particular loaded level. The Display array of screens are reset and a
 * GameScreen and StatsScreen are added as per the Puzzle Mode requirements. It holds a
 * ButtonListener to accept user input for the levels, and to set listeners accordingly.
 * @author David Li
 *
 */

public class PuzzleLevelScreen extends JPanel{
	public PuzzleEngine _Engine;
	public ListPanel levelList;
	private ButtonListener buttonListener;
	
	public PuzzleLevelScreen(PuzzleEngine theEngine, ArrayList<Integer> levelStatuses){
		setOpaque(false);
		buttonListener = new ButtonListener();
		buttonListener.start();
		_Engine = theEngine;
		
		setSize(1000, 420);
		setLayout(null);
		JLabel titleLabel = new JLabel(new ImageIcon(Display.chooseALevelImage));
		titleLabel.setSize(500, 500);
		titleLabel.setOpaque(false);
		titleLabel.setLocation(-50, -210);
		add(titleLabel);
		levelList = new ListPanel(1000, 420, levelStatuses);
		add(levelList);
		add(MainMenuScreen.bgPanel);
	}
	
	public void listenForButtons()
	{
		if(levelList != null && levelList.getSelectedLevel() != 0)
		{
			buttonListener = null;
			_Engine.beginLevel(levelList.getSelectedLevel());
		}
	}
	
	public class ButtonListener extends Thread
	{
		public void run()
		{
			while(Thread.currentThread() == buttonListener)
			{
				try
				{
					Thread.sleep(100);
				}
				catch(InterruptedException e)
				{
					break;
				}
				listenForButtons();
			}
		}
	}
}
